$(document).ready(function () {


    var height = $(".fixed-top").height();
    $(".goods-container").css("padding-top",(height-40)+"px");

    height = $(window).height();
    var width = $(window).width();
    $(".trans-container").css("height",height+"px").show();
    $(".trans-container textarea").css("height",(height*2/5)+"px");

    $(".mask").css("height",height+"px");
    $(".mask").css("width",width+"px");

    //娣诲姞鍒扮嚎鎶ュ垪琛�
    $(document).on("click",".goods-list.thread a",function () {
        // $(this).find(".btn-add-thread").trigger("click");
        return false;
    });

    $(".navigation .category").click(function () {
        showCategoryMenu();
    });

    //娣诲姞鍒扮嚎鎶ュ垪琛�
    $(document).on("click",".goods-list .btn-add-thread",function () {
        var $this = $(this);
        var text = $this.text();
        console.log("#######################");
        var max = 9;
        if (text == "鍔犲叆绾挎姤") {

            var len = $(".thread-container .list").length;
            if (len >= max){
                toast("绾挎姤鏈€澶氭坊鍔�" + max + "涓晢鍝�");
                return false;
            }
            var $goods = $this.parents(".list");
            var url = $goods.data("url");
            var id = $goods.data("id");

            addToThreadList($this);
            console.log(url);
            if (url === undefined) {
                $.ajax({
                    type: 'GET',
                    url: "recommend.htm?messsage=0&id=" + id,
                    dataType: 'json',
                    async:true,
                    success: function (data) {
                        if (data.errcode == "0") {
                            var goods = $(".goods-" + id);
                            $(".goods-" + id).data("url",data.recommendModel.url);
                            $(".goods-" + id).data("image",data.recommendModel.image);
                            url = data.recommendModel.url;
                        }else{
                            toast('娣诲姞绾挎姤澶辫触');
                            removeFromThreadList($this);
                            // toast(data.statusDesc,'text');
                        }
                    },
                    error: function (e) {
                    }
                });
            }else{
                // addToThreadList($this);
            }

            // if (url === undefined) {
            //     toast('娣诲姞绾挎姤澶辫触','text');
            //     return false;
            // }

        }else{

            removeFromThreadList($this);
        }
        return false;
    });

    //鐢熸垚绾挎姤
    $(document).on("click",".btn-newspaper",function () {
        //灞曠ず绾挎姤鎸夐挳
        console.log("click btn-thread......")

        $(this).hide();
        $('.btn-agent').hide();
        $('.xb_tips').show();

        $(".goods-list").addClass("thread");
        // $(this).removeClass("btn-thread");
        // $(this).addClass("btn-back");

        threadImageShared = 0;
        threadCopied = 0;

    });

    // 杩斿洖鎸夐挳
    $(document).on("click",".tool-container .xb-back",function () {
        //鍒囨崲娣诲姞绾挎姤鎸夐挳
        console.log("click btn-back ........")
        $(this).removeClass("btn-back");
        $(".goods-list").removeClass("thread");
        $(".goods-list .btn-add-thread").text("鍔犲叆绾挎姤");
        $(".goods-list .btn-add-thread").removeClass("btn-cancel");

        $(".main").show();
        $(".thread-container").hide();
        $(".thread-container .list").remove();

        $('.goods-list .xb-no').hide().text("");

        //闅愯棌杩斿洖
        $(".tool-container .thread-btn-group").hide();
        // $(".tool-container .btn-trans").show();

        $(".btn-newspaper").show();
        $('.btn-agent').show();
        $('.xb_tips').hide();
    });

    //鍒囨崲鍒颁竾鑳借浆閾�
    $(".tool-container .btn-trans").click(function () {
        location.href = "./trans.htm"
        /*
        var height = $(window).height();
        console.log("height:"+height);
        $(".container").hide();
        $(".trans-container").css("height",height+"px").show();
        // $(".trans-container").show();
        $(".trans-container textarea").css("height",(height/2)+"px");
        */
    });

    //杩斿洖鎸夐挳锛屽彇娑堢嚎鎶�
    $(".thread-container .btn-back").click(function () {
        console.log("click .thread-container .btn-back");
        $(".main").show();
        $(".thread-container").hide();
        $(".tool-container").show();

        // console.log($(".btn-newspaper").isShow());
    });

    //杩斿洖鎸夐挳锛屽彇娑堢嚎鎶�
    $(document).on("click",".thread-container .list button",function () {
        console.log("remove .......");
        removeFromThreadList($(this));
        return false;
    });


    //缂栬緫绾挎姤
    $(".tool-container .btn-thread-edit").click(function () {
        var height = $(window).height();
        console.log("height:"+height);
        $(".tool-container").hide();
        $(".main").hide();
        $(".thread-container .thread-seq").remove();
        $(".thread-container").show();
        return false;
    });

    //杩斿洖鎸夐挳锛屽彇娑堣浆閾�
    $(".trans-container .btn-back").click(function () {
        location.href = "./index.htm"
    });

    //杞摼閲嶇疆
    $(".trans-container .btn-reset").click(function () {
        $("#document_content").val("");
        $(".trans-container .btn-trans").show();
        $(".trans-container .btn-copy").hide();
    });


    //涓囪兘杞摼
    $(".trans-container .btn-trans").click(function () {
        // $(".trans-container .btn-copy").hide();
        var content = $("#document_content").val().trim();
        if (content==""){
            return false;
        }
        showLoading("杞摼涓�");
        $.ajax({
            type: 'POST',
            url: "trans.htm",
            data: {"content":content},
            dataType: 'json',
            success: function (data) {
                hideLoading();
                console.log("",data);
                if (data.errcode == "0") {
                    if (typeof (data.data) =="object") {
                        toast("杞摼鎴愬姛<br/>璇峰鍒�");
                        $("#document_content").data("result", data.data.wenan);
                        $("#document_content").val(data.data.wenan);
                        $(".trans-container .btn-copy").css({"display": "block"});
                        $(".trans-container .btn-trans").hide();
                    }else{
                        toast('杞摼澶辫触','text');
                    }
                }else{
                    toast(data.statusDesc,'text');
                }
            },
            error: function (e) {
                hideLoading();
                toast("杞摼澶辫触",'text');
            }
        });
    });



    //绾挎姤鍥剧墖鍒嗕韩
    $(".thread-container .btn-share-image,.tool-container .btn-share-image").click(function () {
        showLoading("鍥剧墖鐢熸垚涓�");
        var $goodsList = $(".thread-container .list");
        if ($goodsList.length==0){
            return false;
        }
        var $this = $(this);

        var threads = [];
        $goodsList.each(function (i) {
            threads.push($(this).data("id"));
        });
        $.ajax({
            type: 'POST',
            url: "threadimage.htm",
            data: {"ids":threads.join(",")},
            dataType: 'json',
            success: function (data) {
                hideLoading();
                console.log("",data);
                if (data.errcode == "0") {
                    toast("鍥剧墖鍒嗕韩鎴愬姛");

                    if ($this.parents(".tool-container").length>0) {
                        //宸︿晶鎮诞鎸夐挳锛岃嚜鍔ㄨ繑鍥�
                        threadImageShared = 1;
                        if (threadCopied == 1) {
                            $(".tool-container .btn-back").trigger("click");
                        }
                    }

                }else{
                    toast(data.statusDesc,'text');
                }
            },
            error: function (e) {
                hideLoading();
                toast("鍥剧墖鍒嗕韩澶辫触");
            }
        });
    });

    initThreadCopy();
    initTransCopy();
});

var threadImageShared = 0;
var threadCopied = 0;

function initTransCopy() {
    var clipboard = new ClipboardJS(".trans-container .btn-copy", {
        text: function (trigger) {
            var content = $("#document_content").data("result");
            return content;
        }
    });

    clipboard.on('success', function (e) {
        toast("澶嶅埗鎴愬姛");
        $(".trans-container .btn-copy").hide();
        $(".trans-container .btn-trans").show();
        // $("#document_content").val("");
        e.clearSelection();
    });
    clipboard.on('error', function (e) {
        console.log("error",e);
        toast("澶嶅埗澶辫触");
        e.clearSelection();
    });
}


function initThreadCopy() {
    var clipboard = new ClipboardJS(".tool-container .btn-thread-copy,.thread-container .btn-copy", {
        text: function (trigger) {

            var $goodsList = $(".thread-container .list");
            if ($goodsList.length==0){
                return "";
            }

            var threads = [];
            $goodsList.each(function (i) {
                if ($(this).data("url") == undefined){
                    threads = []
                    toast('杩樺湪杞摼涓紝璇风◢鍚庡鍒�');
                    return "";
                }
                var that = $(this);
               // var content = "1锔忊儯 ".replace("1",(i+1)) + $(this).data("goods_name") + "\n";
               // content += "馃挵" + $(this).data("price") + " 馃墣 " + $(this).data("url");
               var goods_name = $(this).data("goods_name");
               if (goods_name.indexOf('銆�')<0){
                   goods_name = '銆�' + goods_name + '銆�'
               }
               var content = "馃拵"+ goods_name + "\n";
               content += $(this).data("price") + " 馃憠 " + $(this).data("url");
               threads.push(content);
            });

            if (threads.length == 0){
                return "";
            }
            // threads.push("馃巵浜笢鍟嗗煄锛屽搧璐ㄤ繚璇侌煄�");

            var content = threads.join("\n\n");
            // var content = threads.join("\n鈥斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€擻n");
            return content;
        }
    });

    clipboard.on('success', function (e) {
        toast("澶嶅埗绾挎姤鎴愬姛");
        if ($(e.trigger).parents(".tool-container").length>0) {
            //宸︿晶鎮诞鎸夐挳锛岃嚜鍔ㄨ繑鍥�
            threadCopied = 1;
            if (threadImageShared == 1) {
                $(".tool-container .btn-back").trigger("click");
            }
        }
        e.clearSelection();
    });
    clipboard.on('error', function (e) {
        console.log("error",e);
        toast("澶嶅埗绾挎姤澶辫触");
        e.clearSelection();
    });
}

/**
 * 娣诲姞鍒扮嚎鎶ュ垪琛�
 * @param $this
 */
function addToThreadList($this) {

    threadImageShared = 0;
    threadCopied = 0;

    var $goods = $this.parents(".list");
    var id = $goods.data("id");
    var theads = $(".thread-container .list");
    var exists = false;
    var idx = theads.length+1;
    theads.each(function (i) {
       var gid = $(this).data("id");
       if (id == gid ){
           idx = (i +1);
           exists = true;
       }
    });

    $(".goods-" + id + " .btn-add-thread").each(function() {
        if (!$(this).hasClass("btn-cancel")) {
            $(this).text("鐐瑰嚮绉婚櫎");
            $(this).data("seq",idx);
            $(this).addClass("btn-cancel");
            $(this).siblings('.xb-no').addClass("thread-seq").text(idx).show();
        }
    });

    if (!exists){
        console.log("url:" + $goods.data("url"));
        var html = $this.parents(".list").prop("outerHTML");
        console.log(html);
        var $new_thread = $(html);
        // $new_thread.data("url",$goods.data("url"));
        $new_thread.find("a").data("url",$goods.data("url"));
        $new_thread.find(".xb-no").remove();
        $new_thread.find(".btn-add-thread").remove();
        $new_thread.find(".extension_btn").text("绉婚櫎");
        $new_thread.find(".extension_btn").data("seq",idx);
        $new_thread.find(".extension_btn").removeClass("extension_btn");

        $(".thread-container .list-box").append($new_thread);

    }

    $("#xb_tips_num").text($(".thread-container .list").length);
    $(".tool-container .thread-btn-group").show();
}

function removeFromThreadList($this) {

    console.log($this);

    threadImageShared = 0;
    threadCopied = 0;

    var id = $this.parents(".list").data("id");
    var seq = parseInt($this.data("seq"));

    $(".goods-" + id + " .btn-cancel").text("鍔犲叆绾挎姤").removeClass("btn-cancel");
    $(".goods-" + id + " .xb-no").removeClass("thread-seq").text("").hide();

    $(".goods-list .thread-seq").each(function () {
        var num = parseInt($(this).text());
        if (num>seq){
            num = num - 1;
            $(this).text(num);
            $(this).parents(".list").find(".btn-cancel").data("seq",num);
        }
    });

    $(".thread-container .goods-" + id).remove();

    var len = $(".thread-container .list").length;
    $("#xb_tips_num").text(len);
    if (len == 0) {
        $(".tool-container .thread-btn-group").hide();
    }


}

/**
 * 鍔犺浇鍟嗗搧鍚庯紝鏇存柊绾挎姤鎸夐挳
 * @returns {boolean}
 */
function reloadThreadList() {

    if (!$(".tool-container .xb-back").is(":visible")){
        return false;
    }

    var theads = $(".thread-container .list");
    theads.each(function (i) {
        var gid = $(this).data("id");
        var idx = i+1;
        $(".goods-" + gid + " .btn-add-thread").each(function() {
            if (!$(this).hasClass("btn-cancel")) {
                $(this).text("鐐瑰嚮绉婚櫎");
                $(this).data("seq",idx);
                $(this).addClass("btn-cancel");
                $(this).siblings('.xb-no').addClass("thread-seq").text(idx).show();
            }
        });
    });

}

function initCopy() {
    var clipboard = new ClipboardJS(".recommended .btn-recommend,.recommended .extension_btn", {
        text: function (trigger) {
            console.log("", trigger);
            var wenan = $(trigger).parents(".list").data("wenan");
            return wenan;
        }
    });

    clipboard.on('success', function (e) {
        toast("澶嶅埗鎴愬姛");
        e.clearSelection();
    });
    clipboard.on('error', function (e) {
        console.log("error",e);
        toast("澶嶅埗澶辫触");
        e.clearSelection();
    });
}

/**
 * 涓€閿帹骞�
 * @param obj   鎸夐挳瀵硅薄
 */
function getRecommendWenan(obj){
    var $this = $(obj);
    var $goods = $this.parents(".list");
    var wenan = $goods.data("wenan");
    var id = $goods.data("id");
    if (wenan === undefined) {
        showLoading("鑾峰彇鎺ㄥ箍閾炬帴涓�");
        $.ajax({
            type: 'GET',
            url: "recommend.htm?id=" + id,
            dataType: 'json',
            success: function (data) {
                layer.closeAll();
                if (data.errcode == "0") {
                    toast('鎺ㄥ箍淇℃伅鎴愬姛');
                    var goods = $(".goods-" + id);
                    goods.addClass("recommended");
                    goods.find(".extension_btn").text("澶嶅埗鏂囨");
                    goods.data("wenan",data.recommendModel.wenan);
                    goods.data("url",data.recommendModel.url);
                    goods.data("image",data.recommendModel.image);
                    initCopy();
                }else{
                    toast(data.statusDesc,'text');
                }
            },
            error: function (e) {
                layer.closeAll();
                // toast("涓€閿帹骞垮け璐ワ紝璇锋鏌ョ綉缁�",1000);
            }
        });
    }
    return wenan;
}

function showCategoryMenu() {
    console.log("showCategoryMenu");
    $(".mask").show();
    var $category = $(".goods-category");
    var width = $category.width();
    $category.css("right",(0-width)+ "px");
    $category.show();
    $category.animate({"right":"0px"})
}

function hideCategoryMenu() {
    console.log("hideCategoryMenu");
    $(".mask").hide();
    var $category = $(".goods-category");
    var width = $category.width();
    $category.animate({"right":(0-width)+"px"});
}